<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
  >
    <el-form ref="formAdd" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="站点头像" prop="avatar">
          <el-input
            clearable
            v-model.trim="form.avatar"
            size="mini"
            style="width: 600px"
            type="textarea"
            :rows="3"
            maxlength="255"
            show-word-limit
            placeholder="请输入在线图片地址"
          />
          <mk-upload v-model="form.avatar" />
        </el-form-item>
        <el-form-item label="站点背景图" prop="wallpaper">
          <el-input
            clearable
            v-model.trim="form.wallpaper"
            size="mini"
            style="width: 600px"
            type="textarea"
            :rows="3"
            maxlength="255"
            show-word-limit
            placeholder="请输入在线图片地址"
          />
        </el-form-item>
        <el-form-item label="是否新年" prop="isNewYear">
          <el-radio-group
            size="mini"
            class="input-one"
            clearable
            placeholder="请选择是否新年"
            v-model="form.isNewYear"
          >
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="站点名称" prop="name">
          <el-input
            v-model="form.name"
            size="mini"
            class="input-one"
            clearable
            placeholder="请输入站点名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="站点格言" prop="slogan">
          <el-input
            clearable
            v-model.trim="form.slogan"
            size="mini"
            style="width: 600px"
            type="textarea"
            :rows="2"
            maxlength="200"
            show-word-limit
            placeholder="请输入站点格言"
          />
        </el-form-item>
        <el-form-item label="站点主页" prop="domain">
          <el-input
            clearable
            v-model.trim="form.domain"
            size="mini"
            style="width: 600px"
            type="textarea"
            :rows="2"
            maxlength="200"
            show-word-limit
            placeholder="请输入站点主页"
          />
        </el-form-item>
        <el-form-item label="站点通知" prop="notice">
          <el-input
            clearable
            v-model.trim="form.notice"
            size="mini"
            style="width: 600px"
            type="textarea"
            :rows="2"
            maxlength="200"
            show-word-limit
            placeholder="请输入站点通知"
          />
        </el-form-item>
        <el-form-item label="站点描述" prop="desc">
          <el-input
            clearable
            v-model.trim="form.desc"
            size="mini"
            style="width: 600px"
            type="textarea"
            :rows="2"
            maxlength="200"
            show-word-limit
            placeholder="请输入站点描述"
          />
        </el-form-item>
        <el-form-item label="全局样式" prop="globalStyle">
          <el-input
            clearable
            v-model.trim="form.globalStyle"
            size="mini"
            style="width: 600px"
            type="textarea"
            :rows="2"
            maxlength="200"
            show-word-limit
            placeholder="filter的值：grayscale(50%)"
          />
        </el-form-item>
      <el-divider />
      <mk-get-button @save="save" @close="handleClose" />
    </el-form>
  </el-dialog>
</template>

<script>
import MkGetButton from "@/components/common/mk-get-button";
import MkBackList from "@/components/common/mk-back-list";
import {
  dirWebsiteCreate,
  dirWebsiteUpdate,
} from "@/api/modules/sys.website.api";
import MkUpload from "@/components/common/mk-upload";

export default {
  name: "get-website-info",
  components: { MkUpload, MkBackList, MkGetButton },
  props: {
    updateData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      dialogTitle: "",
      type: null,
      dialogVisible: false,
      commonKey: 0,
      form: {
        avatar:
          "https://bpic.51yuansu.com/pic3/cover/04/05/51/6135ec9add9eb_260.jpg",
        wallpaper: "https://s1.ax1x.com/2020/05/23/YxaLMq.jpg",
        isNewYear: false,
        name: "",
        slogan: "",
        domain: "",
        notice: "",
        desc: "",
        globalStyle: "",
      },
      isAdd: true,
      rules: {
        avatar: [
          { required: true, message: "请输入站点头像", trigger: "blur" },
        ],

        name: [{ required: true, message: "请输入站点名称", trigger: "blur" }],

        domain: [
          { required: false, message: "请输入站点主页", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    open(type, typeName, data) {
      this.resetForm();
      this.type = type;
      this.dialogTitle = typeName;
      this.dialogVisible = true;
      if (data) {
        this.form = data;
      }
    },
    save() {
      this.$refs["formAdd"].validate((valid) => {
        if (valid) {
          let _FUC = "";
          _FUC = this.type == "create" ? dirWebsiteCreate : dirWebsiteUpdate;
          _FUC(this.form)
            .then((res) => {
              this.$toast.success(
                this.type == "create" ? "添加成功！" : "修改成功！"
              );
              this.dialogVisible = false;
              this.$emit("success");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.dialogVisible = false;
        })
        .catch((_) => {});
    },
    resetForm() {
      this.form = {
        avatar:
          "https://bpic.51yuansu.com/pic3/cover/04/05/51/6135ec9add9eb_260.jpg",
        wallpaper: "https://s1.ax1x.com/2020/05/23/YxaLMq.jpg",
        isNewYear: false,
        name: "",
        slogan: "",
        domain: "",
        notice: "",
        desc: "",
        globalStyle: "",
      };
    },
  },
};
</script>

<style lang="scss" scoped></style>
